#include<string.h>
#include<stdlib.h>
#include "../include/general_vfs_op.h"

int validate_name(char name[])
{
    char *invalid="/";
    if(strpbrk(name,invalid)!=NULL)
        return 0;
    else
        return 1;
}

int if_valid_args_(char *p)
{
   
    if(p==NULL || strlen(p)<=0)
    {
        return 0;
    }
    return 1;
}

fdesc* insertIntoFD(char *path, char *filename,char *filetype)
{
    fdesc* dir=NULL;
    dir = (fdesc*) malloc(sizeof(fdesc));
    strcpy(dir->f_name, filename);
    strcpy(dir->f_path, path);
    strcpy(dir->f_type,filetype);
    dir->f_size=0;
    dir->block_number=-1;
    return dir;
}

int if_valid_flag_(int num)
{
    if(num==NULL)
        return 0;
    else
        return 1;
}


int populate(hashtable* ht,n_tree* n_root,b_node* bst,fdesc* fd)
{
    int n_ins_val,bst_ins_val,bst_del_val,n_del_val;

    n_ins_val=insert_node(&n_root,fd);
    if(n_ins_val==1)
    {
        bst_ins_val=bst_insert(bst,fd);
        if(bst_ins_val)
        {
            if(insert_hashtable(ht,fd))
            {
                return 1;/*hash success*/
            }
            else
            {
                /*hash insert failure*/
                bst_del_val=bst_remove(bst,fd);
                n_del_val= delete_node(&n_root,fd);
                return 0;
            }
        }
        else
        {
            /*bst insert failure*/
            n_del_val=delete_node(&n_root,fd);
            return 0;
        }
    }
    else if(n_ins_val==2)
    {
        bst=NULL;
        bst=(b_node *)malloc(sizeof(b_node));
        datastr->bst=bst;
        bst->key=(char*)malloc(sizeof(char)*CHARSIZE);

        if(init_bst(bst,n_root->data))
        {
            if(bst_recursive_insert(bst,n_root->child))
            {
                ht=NULL;
                ht=(hashtable *)malloc(sizeof(hashtable));
                datastr->htable=ht;
                initialize_hashtable(ht);
                if(ht_recursive_insert(ht,n_root->child))
                    return 10;
                else
                    return 7;
                return 10;
            }
            else return 7;
        }
        else return 7;
    }
    else return n_ins_val;
}


int remove_fd_(hashtable* ht,n_tree* n_root,b_node* bst,fdesc* fd)
{
    int n_ins_val,bst_ins_val,n_del_val,bst_del_val;
    n_ins_val=delete_node(&n_root,fd);
    if(n_ins_val==1)
    {
        bst_ins_val==bst_remove(bst,fd);
        if(bst_ins_val)
        {
            
            if(delete_hashtable(ht,fd))
            {
                
                return 1;
            }
            else
            {
                bst_del_val=bst_insert(bst,fd);
                n_del_val=insert_node(&n_root,fd);
                return 0;
            }
         }
        else
        {
            n_del_val=insert_node(&n_root,fd);
            return 0;

        }
    }
    else
    {
        return  n_ins_val;
    }
    return 1;
}


int bst_recursive_insert(b_node *bst,n_tree *root)
{
    static int res=-1;
    if(root!=NULL)
    {
        res=bst_insert(bst,root->data);
        res=bst_recursive_insert(bst,root->siblings);
        res=bst_recursive_insert(bst,root->child);
    }
    return res;
}

int ht_recursive_insert(hashtable* ht,n_tree *root)
{
    int res=-1;
    if(root!=NULL)
    {
        res=insert_hashtable(ht,root->data);
        res=ht_recursive_insert(ht,root->siblings);
        res=ht_recursive_insert(ht,root->child);
    }
    return res;
}

void remove_last_slash_(char *path){
char *P1;
int length;
P1=(char *)malloc(sizeof(char)*1024);
length=strlen(path);
if(length>1 && path[length-1]=='/'){
  strncpy(P1,path,length-1);
memset(path,'\0',length);
strcpy(path,P1);
}
free(P1);
}


